package com.yyge.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * web容器启动时，会自动调用AbstractDispatcherServletInitializer类来初始化web容器
 */
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        // 指定spring上下文核心配置类
        return new Class[]{SpringConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // 指定spring mvc配置类
        return new Class[]{SpringMvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        // 配置要映射的url路径， / 代表映射所有的请求
        // http://localhost:8080/manager/
        return new String[]{"/"};
    }

    /**
     * 注册自定义的过滤器
     * @return
     */
    @Override
    protected Filter[] getServletFilters() {
        System.out.println("-------------getServletFilters-------------");
        // 解决响应的中文乱码的问题
        CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
        encodingFilter.setEncoding("utf-8");
        return new Filter[]{
                encodingFilter,
                new CORS_Filter()
        };
    }
}
